from flask import Blueprint, render_template, request, jsonify
from flask_login import current_user
from sqlalchemy import desc

from applications.common import curd
from applications.common.curd import model_to_dicts
from applications.common.utils import validate
from applications.common.utils.http import success_api, fail_api, table_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import str_escape
from applications.extensions import db
from applications.models import Dept, User, CompanyJob, JobhunterResume
from applications.schemas.jobhunter_resume import JobhunterResumeSchema
from applications.view.system.native_sql import NativeSQL

bp = Blueprint('jobhunter', __name__, url_prefix='/jobhunter')
user = 'lucas'
password = '5515612'
dbname = 'pearadminflask'
nativesql = NativeSQL(user, password, dbname)

@bp.get('/')
@authorize("system:jobhunter:main")
def main():
    return render_template('system/jobhunter/main.html')

@bp.get('/data')
@authorize("system:jobhunter:main")
def data():
    current_username = current_user.username
    print(current_username)


    sql = 'select b.role_id from admin_user a left join admin_user_role b on a.id = b.user_id where a.username = "{}"'.format(current_username)
    code, msg, data = nativesql.query(sql)
    print(code, msg, data)
    role_id = data[0]['role_id']
    if 4 == role_id:
        resdata = JobhunterResume.query.filter_by(username=current_username).layui_paginate()
        count = resdata.total
        return table_api(data=model_to_dicts(schema=JobhunterResumeSchema, data=resdata.items), count=count)
    elif 3 == role_id:
        resdata = JobhunterResume.query.filter_by(username=current_username).layui_paginate()
        sql = 'select jobhunter_toudi_tb.*, company_job_tb.* from jobhunter_toudi_tb, company_job_tb ' \
              'where jobhunter_toudi_tb.jobid = company_job_tb.id and company_job_tb.username = "{}"'.format(current_username)
        code, msg, resdata = nativesql.query(sql)
        print(sql)
        print(resdata)
        count = len(resdata)
        res = {
            'msg': msg,
            'code': 0,
            'data': resdata,
            'count': count,
            'limit': 10

        }
        return jsonify(res)
    else:
        resdata = JobhunterResume.query.layui_paginate()
        count = resdata.total
        return table_api(data=model_to_dicts(schema=JobhunterResumeSchema, data=resdata.items), count=count)

@bp.get('/add')
@authorize("system:jobhunter:add", log=True)
def add():
    return render_template('system/jobhunter/add.html')

@bp.post('/save')
@authorize("system:jobhunter:add", log=True)
def save():
    req_json = request.get_json(force=True)
    print(req_json)
    job = JobhunterResume(username=current_user.username,
                          realname=req_json['realname'],
                          mobilephone=req_json['mobilephone'],
                          age=req_json['age'],
                          education=req_json['education'],
                          experience=req_json['experience'],
                          other=req_json['other'])
    db.session.add(job)
    db.session.commit()
    return success_api(msg="增加成功")

@bp.delete('/remove/<int:id>')
@authorize("system:jobhunter:remove", log=True)
def delete(id):
    print(id)
    res = JobhunterResume.query.filter_by(id=id).delete()
    db.session.commit()
    if not res:
        return fail_api(msg="删除失败")
    return success_api(msg="删除成功")
